<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/28 0028
 * Time: 下午 11:47
 */

namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use backend\models\User;
use backend\models\UserGroup;
use backend\models\WebConfig;

class ApiController extends Controller{
    public $layout = false;
    public function actionMemberlist()
    {
        $wechat = Yii::$app->wechat;
        $data = $wechat->getMemberList();
        foreach($data['data']['openid'] as $value){
            $model = new User();
            $model->oppenid = $value;
            $model->save();
        }
    }

    public function actionMemberinfo(){
        $wechat = Yii::$app->wechat;
        $users = User::find()->all();
        $model = $wechat->getMemberInfo(18);
        foreach($users as $user){
            $model_mem = User::findOne($user->id);
            $model = $wechat->getMemberInfo($user['oppenid']);
            $model_mem->setAttributes($model);
            if(!$model_mem->save()){
                print_r($model_mem->getErrors());
            }
        }
    }

    public function actionGetgroups(){
        $wechat = Yii::$app->wechat;
        $models = $wechat->getGroupList();
        foreach($models as $value){
            $model = UserGroup::find()->where(['id'=>$value['id']])->one();
            if(!$model){
                $model = new UserGroup;
            }
            $model->setAttributes($value);
            $model->save();
        }
    }

    public function actionCreatemenu(){
        $wechat = Yii::$app->wechat;
        $data = [
            [
                'type'=>'click',
                'name'=>'第一个菜单',
                'key'=>'http://wxtest.liangloo.com',
            ],
            [
                'type'=>'click',
                'name'=>'第二个菜单',
                'key'=>'http:://wxtest.liangloo.com',
            ]
        ];
        $result = $wechat->createMenu($data);
        if(!$result){
            var_dump($wechat->lastErrorInfo);
        }
    }

    public function actionGetmenulist(){
        $wechat = Yii::$app->wechat;
        $result = $wechat->getMenuList();
        if(!$result){
            var_dump($wechat->lastErrorInfo);
        }
    }

    public function actionAccesstoken(){
        $wechat = Yii::$app->wechat;
        $model = WebConfig::find()->one();

        $token = $wechat->getAccessToken();
        if( $token || $model->accesstoken != $token){
            $model->accesstoken = $token;
            $model->save();
        }
    }
    
    public function actionJsapi(){
        $wechat = Yii::$app->wechat;
        return $this->render('jsapi',['wechat'=>$wechat]);
    }

    public function actionSendmessage(){
        $wechat = Yii::$app->wechat;
        $user = User::findOne(1);
        $data = [  "first" => [
                "value" => "恭喜你购买成功！",
                "color" => "#173177"
               ],
            "product" => [
                "value" => "巧克力",
                "color" => "#173177"
               ],
            "price" => [
                "value" => "39.8元",
                "color" => "#173177"
               ],
            "time" => [
                "value" => "2014年9月16日",
                "color" => "#173177"
               ],
            "remark" => [
               "value" => "欢迎再次购买！",
                "color" => "#173177"
               ],
            ];
        var_dump($wechat->sendTemplateMessage($user['oppenid'],1,$data));
    }
    

    public function actionShorturl(){
        $wechat = Yii::$app->wechat;
        $url = "http://wxtest.liangloo.com";
        $url = $wechat->createShortUrl($url);
        var_dump($url);
    }

    public function actionOauth(){
        $wechat = Yii::$app->wechat;
        $redirectUrl = "http://www.baidu.com";

        $result = $wechat->getOauth2AuthorizeUrl($redirectUrl,'wxtest','snsapi_userinfo');
        $this->redirect($result);
    }
    
    public function actionSendarticles(){
        $wechat = Yii::$app->wechat;
        $user = User::findOne(1);

        $target = [
            'filter' => '0',
        ];
        var_dump($wechat->sendArticles($target,"第一条群发消息测试",'text'));
    }
}
